package com.hctforgreen.greenservice.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.hctforgreen.greenservice.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 开机密码提醒内容及资料关联实体类
 * 
 * @author denghb
 * 
 */
public class RelevanceEntity extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public String content;
	public ArrayList<BookClass> bookList;
	public ArrayList<VedioClass> vedioList;
	public int flag;
	private static RelevanceEntity relevanceEntity = new RelevanceEntity();


	public static RelevanceEntity getInstance(){
		return relevanceEntity;
	}
	public class BookClass implements Serializable{
		public String bookId;
		public String bookName;

		public BookClass(){
			super();
		}
		public String getBookId() {
			return bookId;
		}

		public void setBookId(String bookId) {
			this.bookId = bookId;
		}

		public String getBookName() {
			return bookName;
		}

		public void setBookName(String bookName) {
			this.bookName = bookName;
		}
	}

	public class VedioClass implements Serializable{
		public String vedioId;
		public String vedioName;

		public VedioClass(){
			super();
		}

		public String getVedioId() {
			return vedioId;
		}

		public void setVedioId(String vedioId) {
			this.vedioId = vedioId;
		}

		public String getVedioName() {
			return vedioName;
		}

		public void setVedioName(String vedioName) {
			this.vedioName = vedioName;
		}
	}


	public  RelevanceEntity parse(JSONObject jsonObject)
			throws JSONException {
		RelevanceEntity entity = new RelevanceEntity();
		if (jsonObject == null) {
			return entity;
		}
		if (jsonObject.has("content")) {
			entity.content = jsonObject.getString("content");
		}
		if (jsonObject.has("flag")){
			entity.flag = Integer.parseInt(jsonObject.getString("flag"));
		}

		entity.bookList=new ArrayList<BookClass>();

		if (jsonObject.has("bookList")){
			JSONArray jsonArray = jsonObject.getJSONArray("bookList");
			if (jsonArray != null && jsonArray.length() > 0){
				for (int i = 0;i < jsonArray.length();i++){
					BookClass bookClass = new BookClass();
					JSONObject jsonObj = jsonArray.getJSONObject(i);
					LogUtil.e("pm","RelevanceEntity:book json obj=" + i + jsonObj);
						if (jsonObj.has("bookId")) {
							if (TextUtils.isEmpty(jsonObj.getString("bookId"))) {
								LogUtil.e("pm","RelevanceEntity:bookid=" + i + "null");
								continue;
							}else{
								bookClass.setBookId(jsonObj.getString("bookId"));

							}
						}
						if (jsonObj.has("bookName")) {
							if (TextUtils.isEmpty(jsonObj.getString("bookName"))) {
								LogUtil.e("pm","RelevanceEntity:bookname=" + i + "null");
							}else{
								bookClass.setBookName(jsonObj.getString("bookName"));
							}
						}

					entity.bookList.add(bookClass);
				}
			}
		}

		entity.vedioList=new ArrayList<VedioClass>();
		if (jsonObject.has("videoList")){
			JSONArray jsonArray = jsonObject.getJSONArray("videoList");
			if (jsonArray != null && jsonArray.length() > 0){
				for (int i = 0;i < jsonArray.length();i++){
					VedioClass vedioClass = new VedioClass();
					JSONObject jsonObj = jsonArray.getJSONObject(i);
					LogUtil.e("pm","RelevanceEntity:vedio json obj=" + i + jsonObj);
					if (jsonObj.has("videoId")){
						if (TextUtils.isEmpty(jsonObj.getString("videoId"))) {
							LogUtil.e("pm","RelevanceEntity:vedioId=" + i + "null");
							continue;
						}else{
							vedioClass.setVedioId(jsonObj.getString("videoId"));

						}
					}
					if (jsonObj.has("videoName")){
						if (TextUtils.isEmpty(jsonObj.getString("videoName"))) {
							LogUtil.e("pm","RelevanceEntity:vedioname=" + i + "null");
							continue;
						}else{
							vedioClass.setVedioName(jsonObj.getString("videoName"));
						}
					}

					entity.vedioList.add(vedioClass);
				}
			}
		}

		return entity;
	}

}
